/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.imageview.preview.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.PageSliderIndicator;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.item.ImageViewInfo;
import com.xuexiang.xuidemo.provider.PageSliderAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 九宫格图片滑动
 *
 * @since 2021-04-08
 */
public class BannerNineGridPagerSlice extends AbilitySlice {
    List<ImageViewInfo> itemList = new ArrayList<>();
    List<ImageViewInfo> itemList1 = new ArrayList<>();
    List<ImageViewInfo> itemList2 = new ArrayList<>();
    List<ImageViewInfo> itemList3 = new ArrayList<>();
    ArrayList<String> pages = new ArrayList<>();
    PageSliderIndicator pageIndicator;
    PageSlider pageSlider, pageVideoSlider;
    private String position, index;

    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_banner_nine_grid_pager);
        if (intent != null) {
            position = intent.getStringParam("position");
            index = intent.getStringParam("index");
        }
        initView();
        initData();
    }

    private void initView() {
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        pageIndicator = (PageSliderIndicator) findComponentById(ResourceTable.Id_page_indicator);
        pageVideoSlider = (PageSlider) findComponentById(ResourceTable.Id_page_video_slider);
        pageSlider.setVisibility(Component.VISIBLE);
        pageVideoSlider.setVisibility(Component.HIDE);
        ShapeElement normalElement = new ShapeElement();
        normalElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#FF7E7E7E")));
        normalElement.setAlpha(168);
        normalElement.setShape(ShapeElement.OVAL);
        normalElement.setBounds(0, 0, 25, 25);
        ShapeElement selectedElement = new ShapeElement();
        selectedElement.setRgbColor(RgbColor.fromArgbInt(0xFFFFFFFF));
        selectedElement.setShape(ShapeElement.OVAL);
        selectedElement.setBounds(0, 0, 25, 25);
        pageIndicator.setItemElement(normalElement, selectedElement);
        pageIndicator.setItemOffset(20);
        pageIndicator.setPageSlider(pageSlider);
        pageIndicator.addOnSelectionChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
            }

            @Override
            public void onPageSlideStateChanged(int i) {
            }

            @Override
            public void onPageChosen(int i) {
                pageIndicator.setSelected(i);
            }
        });
    }

    private void initData() {
        if ("0".equals(position)) {
            itemList.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=141294069"
                + ",864810829&fm=26&gp=0.jpg"));
            itemList.add(new ImageViewInfo("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2860640912"
                + ",1833965317&fm=26&gp=0.jpg"));
            for (int i = 0; i < itemList.size(); i++) {
                pages.add(itemList.get(i).getUrl());
            }
            PageSliderAdapter adapter = new PageSliderAdapter(pages, this, this);
            pageSlider.setProvider(adapter);
            pageIndicator.setSelected(Integer.parseInt(index));

        } else if ("1".equals(position)) {
            itemList1.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=141294069"
                + ",864810829&fm=26&gp=0.jpg"));
            itemList1.add(new ImageViewInfo("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2860640912"
                + ",1833965317&fm=26&gp=0.jpg"));
            itemList1.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3933345026"
                + ",3409802114&fm=26&gp=0.jpg"));
            itemList1.add(new ImageViewInfo("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1107220453"
                + ",145887914&fm=26&gp=0.jpg"));
            itemList1.add(new ImageViewInfo("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1107220453"
                + ",145887914&fm=26&gp=0.jpg"));
            itemList1.add(new ImageViewInfo("https://photocdn.sohu.com/20160307/mp62252655_1457334772519_2.png"));
            itemList1.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3933345026"
                + ",3409802114&fm=26&gp=0.jpg"));
            for (int i = 0; i < itemList1.size(); i++) {
                pages.add(itemList1.get(i).getUrl());
            }
            PageSliderAdapter adapter1 = new PageSliderAdapter(pages, this, this);
            pageSlider.setProvider(adapter1);
            pageIndicator.setSelected(Integer.parseInt(index));
        } else if ("2".equals(position)) {
            itemList2.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=141294069"
                + ",864810829&fm=26&gp=0.jpg"));
            itemList2.add(new ImageViewInfo("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2860640912"
                + ",1833965317&fm=26&gp=0.jpg"));
            itemList2.add(new ImageViewInfo("https://photocdn.sohu.com/20160307/mp62252655_1457334772519_2.png"));
            itemList2.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3933345026"
                + ",3409802114&fm=26&gp=0.jpg"));
            for (int i = 0; i < itemList2.size(); i++) {
                pages.add(itemList2.get(i).getUrl());
            }
            PageSliderAdapter adapter2 = new PageSliderAdapter(pages, this, this);
            pageSlider.setProvider(adapter2);
            pageIndicator.setSelected(Integer.parseInt(index));
        } else {
            itemList3.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=141294069"
                + ",864810829&fm=26&gp=0.jpg"));
            itemList3.add(new ImageViewInfo("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2860640912"
                + ",1833965317&fm=26&gp=0.jpg"));
            itemList3.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3933345026"
                + ",3409802114&fm=26&gp=0.jpg"));
            itemList3.add(new ImageViewInfo("https://photocdn.sohu.com/20160307/mp62252655_1457334772519_2.png"));
            itemList3.add(new ImageViewInfo("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1107220453"
                + ",145887914&fm=26&gp=0.jpg"));
            itemList3.add(new ImageViewInfo("https://photocdn.sohu.com/20160307/mp62252655_1457334772519_2.png"));
            itemList3.add(new ImageViewInfo("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2860640912"
                + ",1833965317&fm=26&gp=0.jpg"));
            itemList3.add(new ImageViewInfo("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2860640912"
                + ",1833965317&fm=26&gp=0.jpg"));
            itemList3.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3933345026"
                + ",3409802114&fm=26&gp=0.jpg"));
            for (int i = 0; i < itemList3.size(); i++) {
                pages.add(itemList3.get(i).getUrl());
            }
            PageSliderAdapter adapter3 = new PageSliderAdapter(pages, this, this);
            pageSlider.setProvider(adapter3);
            pageIndicator.setSelected(Integer.parseInt(index));
        }
    }
}
